package com.css.dzj.zfjg.modules.tipinfo.controller;


import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.tipinfo.entity.JgLhjgTipInfoEntity;
import com.css.dzj.zfjg.modules.tipinfo.service.JgLhjgTipInfoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 企业举报信息
 *
 * @author zch
 * @email
 * @date 2020-02-18 09:41:33
 */
@RestController
@RequestMapping("generator/jglhjgtipinfo")
public class JgLhjgTipInfoController {
    @Autowired
    private JgLhjgTipInfoService jgLhjgTipInfoService;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgTipInfoEntity> tipInfoQueryResults = jgLhjgTipInfoService.queryPage(pageRequest);
            baseService.logSuccess(3, "分页查询企业举报信息成功", "2");
            return R.ok().put("data", tipInfoQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询企业举报信息失败", "2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{tipInfoId}")
    public R info(@RequestParam String tipInfoId){
        try {
            JgLhjgTipInfoEntity jgLhjgTipInfo = jgLhjgTipInfoService.findById(tipInfoId);
            baseService.logSuccess(3, "通过id获取企业举报信息成功", "2");
            return R.ok().put("jgLhjgTipInfo", jgLhjgTipInfo);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取企业举报信息失败", "2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgTipInfoEntity jgLhjgTipInfo, HttpServletRequest request){
        try {
            if(StringUtils.isEmpty(jgLhjgTipInfo)) {
                return R.error("企业举报信息不能为空");
            }
            jgLhjgTipInfoService.add(jgLhjgTipInfo, request);
            baseService.logSuccess(3, "企业举报信息添加成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业举报信息添加失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgTipInfoEntity jgLhjgTipInfo){
        try {
            JgLhjgTipInfoEntity oldJgLhjgTipInfoEntity = jgLhjgTipInfoService.findById(jgLhjgTipInfo.getTipInfoId());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgTipInfo, oldJgLhjgTipInfoEntity);
            jgLhjgTipInfoService.update(jgLhjgTipInfo);
            baseService.logSuccess(3, "企业举报信息修改成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业举报信息修改失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam String tipInfoId){
        try {
            JgLhjgTipInfoEntity jgLhjgTipInfoEntity = jgLhjgTipInfoService.findById(tipInfoId);
            jgLhjgTipInfoService.delete(jgLhjgTipInfoEntity);
            baseService.logSuccess(3, "企业举报信息删除成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "企业举报信息删除失败", "2");
            return R.error(e.getMessage());
        }
    }

}
